// pages/temp/index.js
let md5 = require('../../../utils/md5');
import WxValidate from '../../../utils/validate'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {},
    phoneNumber: "",
    countdown: 60,
    isClick: false,
    btnTxt: "获取验证码",
  },
  submit(e) {
    console.log(e)
    let that = this
    if (e.detail.target.id == 'get') {
      this.data.phoneNumber = e.detail.value.phoneNumber
      // 调用验证码
      that.codeBtn()
    } else {
      this.data.user = e.detail.value
      this.confrim()
      // 调用提交接口
    }
  },
  settime() {
    if (this.data.countdown == 0) {
      this.data.countdown = 60;
      this.setData({
        isClick: false,
        btnTxt: "获取验证码"
      })
      return;
    } else {
      let countdown = this.data.countdown;
      let btnTxt = "重新发送(" + countdown + ")";
      this.setData({
        isClick: true,
        btnTxt: btnTxt,
      })
      this.data.countdown--;
    }
    setTimeout(() => {
      this.settime();
    }, 1000);
  },
  getWord() {
    let self = this;
    let phoneNumber = this.data.phoneNumber;
    let obj = {};
    obj.phoneNumber = phoneNumber;
    self.axios.noToken('handle/generatePhoneCode', obj, 'POST').then((res) => {
      console.log(res, '返回值')
      if (res.code == "200") {
        wx.showToast({
          title: '验证码已发送',
          icon: 'success',
          duration: 2000
        })
        self.settime();
      } else {
        // wx.showToast({
        //   title: res.msg,
        //   icon: 'fail',
        //   duration: 2000
        // })
      }
    });

  },
  // 验证码按钮
  codeBtn() {
    let self = this;
    let phoneNumber = this.data.phoneNumber;
    var myreg =
      /^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/;
    if (!myreg.test(phoneNumber)) {
      wx.showModal({
        content: '请输入正确的手机号码',
        showCancel: false,
      })
      return false;
    } else if (!this.isClick) {
      this.getWord();
    }
  },
  // 展示信息
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
  // 提交
  confrim() {
    // 进行规则校检
    let that = this
    let { newPassword, confirmPassword,phoneNumber,phoneCode } = this.data.user
    let temp = this.data.user
    if (!this.WxValidate.checkForm(temp)) {
      let msg = this.WxValidate.errorList[0];
      this.showModal(msg);
      return false
    }
    // 校检新旧密码
    if (newPassword != confirmPassword) {
      wx.showModal({
        content: '两次输入密码不一致',
        showCancel: false,
      })
      return
    }
    // 校检手机号
    var myreg =
      /^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/;
    if (!myreg.test(phoneNumber)) {
      wx.showModal({
        content: '请输入正确的手机号码',
        showCancel: false,
      })
      return false;
    }
    let obj = {}
    obj.password1 = md5(newPassword);
    obj.password2 = md5(confirmPassword);
    obj.phoneNumber = phoneNumber;
    obj.phoneCode = phoneCode;
    this.axios.noToken('handle/updatePassword', obj, 'POST').then((res) => {
      if (res.code == '200') {
        // 修改成功
        wx.showToast({
          title: '更改成功',
          icon: 'success',
          duration: 2000,
          success: () => {
            that.Router.push({
              path: "",
              query: {},
              openType: 'back',
              duration: 2000,
            })
          }
        })
      }
    })
  },
  initValidate() {
    const rules = {
      phoneNumber: {
        required: true,
      },
      phoneCode: {
        required: true,
      },
      newPassword: {
        required: true,
        minlength: 8,
        limitInput: true,
      },
      confirmPassword: {
        required: true,
      },
    }
    const messages = {
      phoneNumber: {
        required: '请输入手机号码',
      },
      phoneCode: {
        required: '请输入验证码',
      },
      newPassword: {
        required: '请输入密码',
        minlength: '密码长度不能少于8位',
      },
      confirmPassword: {
        required: '请输入确认密码',
      },
    }
    let that = this;
    this.WxValidate = new WxValidate(rules, messages)
    this.WxValidate.addMethod('limitInput', (value, param) => {
      return this.WxValidate.optional(value) || /(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9])/.test(value);
    }, '密码包含大小写英文数字和符号')
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 创建校检器
    this.initValidate();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})